package com.wecode.core.utils;


import com.wecode.core.constant.ErrorConstant;
import lombok.Data;

@Data
public class ResponseObject<T> {

    private int code;
    private String msg;

    private T data;

    /**
     * 接口调用成功时返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseObject<T> success(T data) {
        ResponseObject<T> respObject = new ResponseObject<>();
        respObject.setCode(200);
        respObject.setMsg("处理成功");
        respObject.setData(data);
        return respObject;
    }

    /**
     * 返回失败信息
     * 注意：一般出现错误需抛Exception，由ExceptionHandler处理并返回，
     * 只有在特殊情况异常无法被捕捉时才会调用此方法手动返回
     *
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> ResponseObject<T> fail(ErrorConstant errorCode) {
        ResponseObject<T> responseObject = new ResponseObject<>();
        responseObject.setCode(errorCode.getCode());
        responseObject.setMsg(errorCode.getMsg());
        return responseObject;
    }
}
